﻿@layout DeployLayout

@page "/Deploy/DefaultDataList"

@using Materal.APP.Core.Models
@using Deploy.Enums
@using Materal.Common
@using Materal.Model
@using Deploy.DataTransmitModel.DefaultData
@using Deploy.HttpManage
@using Deploy.PresentationModel.DefaultData
@using WebAPP.MateralUI.Models
@using Materal.ConvertHelper
@using WebAPP.Common

@inject MessageManage _messageManage
@inject ExceptionManage _exceptionManage
@inject IDefaultDataManage _defaultDataManage

<main class="layout_body">
    <MPageHeader Title="默认数据配置">
        <div class="m_button_group">
            <MButton Class="m_button_icon Micon Micon_plus" Title="新增" ButtonType="@MButtonTypeEnum.Success" OnClick="@OnBtnAddClick"></MButton>
        </div>
    </MPageHeader>
    <section class="search_panel">
        <MForm FromData="SearchData" Horizontal>
            <MFormItem Title="键">
                <MInput Type="search" Disabled="@Loading" @bind-Value="@context.Key"></MInput>
            </MFormItem>
            <MFormItem Title="应用类型">
                <MSelect Disabled="@Loading" Items="AllApplicationTypes" @bind-Value="@context.ApplicationType"></MSelect>
            </MFormItem>
            <MFormItem>
                <MButton Class="m_button_icon Micon Micon_search" Title="查询" Disabled="@Loading" ButtonType="@MButtonTypeEnum.Primary" OnClick="@OnBtnSearchClick"></MButton>
            </MFormItem>
        </MForm>
    </section>
    <MTable DataSource="TableDataSource" TableConfig="TableConfig">
        <MTableCell>@context.Index</MTableCell>
        <MTableCell>@context.Key</MTableCell>
        <MTableCell>@context.ApplicationTypeString</MTableCell>
        <MTableCell>@context.Data</MTableCell>
        <MTableCell>
            <div class="m_button_group">
                <MButton Class="m_button_icon Micon Micon_edit" Title="修改" OnClick="@(eventArgs=>OnBtnEditClick(eventArgs,context.ID))"></MButton>
                <MButton Class="m_button_icon Micon Micon_delete" Title="删除" ButtonType="@MButtonTypeEnum.Danger" OnClick="@(eventArgs=>OnBtnOpenDeleteModalClick(eventArgs,context.ID))"></MButton>
            </div>
        </MTableCell>
    </MTable>
    <MPaging PageModel="@SearchData.PageModel" PageIndexChanged="@OnPageIndexChangedAsync"></MPaging>
</main>
<MModal Title="@(SelectedID.HasValue ? "修改" : "新增")" @bind-Display="OperationModalDisplay">
    <OperationDefaultData DefaultDataID="SelectedID" Success="@OnOperationSuccessAsync" Cancel="@CloseOperationModal"></OperationDefaultData>
</MModal>
<MModal Title="删除" @bind-Display="DeleteModalDisplay">
    确定要删除该项吗？
    <div class="m_modal_buttons">
        <MButton Disabled="@Loading" OnClick="@CloseDeleteModal">取消</MButton>
        <MButton ButtonType="@MButtonTypeEnum.Danger" Disabled="@Loading" OnClick="@OnBtnDeleteClickAsync">删除</MButton>
    </div>
</MModal>
@code{
    /// <summary>
    /// 查询数据模型
    /// </summary>
    public class DefaultDataSearchFromDataModel
    {
        /// <summary>
        /// 查询应用程序类型
        /// </summary>
        public KeyValueModel ApplicationType { get; set; } = new KeyValueModel(null, "所有类型");
        /// <summary>
        /// 查询键
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 分页模型
        /// </summary>
        public PageModel PageModel { get; set; } = new PageModel
        {
            PageIndex = MateralConfig.PageStartNumber,
            PageSize = 10,
            DataCount = 0
        };
    }
    /// <summary>
    /// 默认数据列表模型
    /// </summary>
    public class DefaultDataListModel : DefaultDataListDTO
    {
        public DefaultDataListModel()
        {

        }
        public DefaultDataListModel(PageRequestModel pageModel, int index)
        {
            Index = (pageModel.PageIndex - MateralConfig.PageStartNumber) * pageModel.PageSize + index + 1;
        }
        public DefaultDataListModel(DefaultDataListDTO listDTO, PageRequestModel pageModel, int index)
        {
            listDTO.CopyProperties(this);
            Index = (pageModel.PageIndex - MateralConfig.PageStartNumber) * pageModel.PageSize + index + 1;
        }
        /// <summary>
        /// 位序
        /// </summary>
        public int Index { get; set; }
    }
}
@code{
    #region 属性
    /// <summary>
    /// 选中的唯一标识
    /// </summary>
    public Guid? SelectedID { get; set; }
    /// <summary>
    /// 操作模态窗是否显示
    /// </summary>
    public bool OperationModalDisplay { get; set; }
    /// <summary>
    /// 删除模态窗是否显示
    /// </summary>
    public bool DeleteModalDisplay { get; set; }
    /// <summary>
    /// 所有应用程序类型
    /// </summary>
    public List<KeyValueModel> AllApplicationTypes { get; set; }
    /// <summary>
    /// 查询数据
    /// </summary>
    public DefaultDataSearchFromDataModel SearchData { get; set; } = new DefaultDataSearchFromDataModel();
    /// <summary>
    /// 加载中
    /// </summary>
    public bool Loading { get; set; } = true;
    /// <summary>
    /// 表格配置
    /// </summary>
    public List<TableConfigModel> TableConfig { get; set; }
    /// <summary>
    /// 表格数据源
    /// </summary>
    public List<DefaultDataListModel> TableDataSource { get; set; }
    #endregion
    #region 生命周期
    /// <summary>
    /// 设置参数后
    /// </summary>
    /// <returns></returns>
    protected override async Task OnParametersSetAsync()
    {
        AllApplicationTypes = KeyValueModel.GetAllCode(typeof(ApplicationTypeEnum));
        AllApplicationTypes.Insert(0, SearchData.ApplicationType);
        TableConfig = new List<TableConfigModel>
    {
            new TableConfigModel("#"),
            new TableConfigModel("键"),
            new TableConfigModel("应用类型"),
            new TableConfigModel("数据",500),
            new TableConfigModel("操作")
        };
        TableDataSource = new List<DefaultDataListModel>();
        await base.OnParametersSetAsync();
    }
    /// <summary>
    /// 页面呈现后
    /// </summary>
    /// <param name="firstRender"></param>
    /// <returns></returns>
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (!firstRender) return;
        await SearchTableDataAsync();
        await base.OnAfterRenderAsync(true);
        StateHasChanged();
    }
    #endregion
    #region 事件
    /// <summary>
    /// 单击新增按钮
    /// </summary>
    /// <param name="eventArgs"></param>
    /// <returns></returns>
    public void OnBtnAddClick(MouseEventArgs eventArgs)
    {
        SelectedID = null;
        OpenOperationModal();
    }
    /// <summary>
    /// 单击修改按钮
    /// </summary>
    /// <param name="eventArgs"></param>
    /// <param name="selectedID"></param>
    /// <returns></returns>
    public void OnBtnEditClick(MouseEventArgs eventArgs, Guid selectedID)
    {
        SelectedID = selectedID;
        OpenOperationModal();
    }
    /// <summary>
    /// 单击删除按钮
    /// </summary>
    /// <param name="eventArgs"></param>
    /// <param name="selectedID"></param>
    /// <returns></returns>
    public void OnBtnOpenDeleteModalClick(MouseEventArgs eventArgs, Guid selectedID)
    {
        SelectedID = selectedID;
        OpenDeleteModal();
    }
    /// <summary>
    /// 单击删除按钮
    /// </summary>
    /// <param name="eventArgs"></param>
    /// <returns></returns>
    public async Task OnBtnDeleteClickAsync(MouseEventArgs eventArgs)
    {
        await DeleteAsync();
    }
    /// <summary>
    /// 单击查询按钮
    /// </summary>
    /// <param name="eventArgs"></param>
    /// <returns></returns>
    public async Task OnBtnSearchClick(MouseEventArgs eventArgs)
    {
        await SearchTableDataAsync();
    }
    /// <summary>
    /// 当前页发生更新
    /// </summary>
    /// <param name="pageIndex"></param>
    /// <returns></returns>
    public async Task OnPageIndexChangedAsync(int pageIndex)
    {
        await SearchTableDataAsync(pageIndex);
    }
    /// <summary>
    /// 修改成功
    /// </summary>
    /// <returns></returns>
    public async Task OnOperationSuccessAsync()
    {
        await SearchTableDataAsync(SearchData.PageModel.PageIndex);
        CloseOperationModal();
    }
    #endregion
    #region 私有方法
    /// <summary>
    /// 打开操作模态框
    /// </summary>
    private void OpenOperationModal()
    {
        OperationModalDisplay = true;
    }
    /// <summary>
    /// 关闭删除操作模态框
    /// </summary>
    private void CloseOperationModal()
    {
        OperationModalDisplay = false;
    }
    /// <summary>
    /// 打开删除模态框
    /// </summary>
    private void OpenDeleteModal()
    {
        DeleteModalDisplay = true;
    }
    /// <summary>
    /// 关闭删除模态框
    /// </summary>
    private void CloseDeleteModal()
    {
        DeleteModalDisplay = false;
    }
    /// <summary>
    /// 查询表格数据
    /// </summary>
    /// <param name="pageIndex"></param>
    /// <returns></returns>
    private async Task SearchTableDataAsync(int pageIndex = 1)
    {
        Loading = true;
        try
        {
            var requestModel = new QueryDefaultDataFilterRequestModel
            {
                Key = SearchData.Key,
                PageIndex = pageIndex,
                PageSize = SearchData.PageModel.PageSize
            };
            if (SearchData.ApplicationType.Key == null)
            {
                requestModel.ApplicationType = null;
            }
            else
            {
                requestModel.ApplicationType = (ApplicationTypeEnum)Convert.ToInt32(SearchData.ApplicationType.Key);
            }
            PageResultModel<DefaultDataListDTO> resultModel = await _defaultDataManage.GetListAsync(requestModel);
            if (resultModel.ResultType == ResultTypeEnum.Success)
            {
                SearchData.PageModel = resultModel.PageModel;
                TableDataSource.Clear();
                DefaultDataListDTO[] dataListDto = resultModel.Data.ToArray();
                for (var i = 0; i < dataListDto.Length; i++)
                {
                    TableDataSource.Add(new DefaultDataListModel(dataListDto[i], SearchData.PageModel, i));
                }
            }
            else
            {
                await _messageManage.WarningAsync(resultModel.Message);
            }
        }
        catch (Exception exception)
        {
            await _exceptionManage.HandlerExceptionAsync(exception);
        }
        finally
        {
            Loading = false;
        }
    }
    /// <summary>
    /// 删除
    /// </summary>
    /// <returns></returns>
    private async Task DeleteAsync()
    {
        Loading = true;
        try
        {
            if (!SelectedID.HasValue) throw new WebAPPException("未选择数据");
            ResultModel resultModel = await _defaultDataManage.DeleteAsync(SelectedID.Value);
            if (resultModel.ResultType == ResultTypeEnum.Success)
            {
                await _messageManage.SuccessAsync(resultModel.Message);
                await SearchTableDataAsync(SearchData.PageModel.PageIndex);
                CloseDeleteModal();
            }
            else
            {
                await _messageManage.WarningAsync(resultModel.Message);
            }
        }
        catch (Exception exception)
        {
            await _exceptionManage.HandlerExceptionAsync(exception);
        }
        finally
        {
            Loading = false;
        }
    }
    #endregion
}